package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Category;
import com.example.demo.entity.Goods;
import com.example.demo.entity.Sku;
import com.example.demo.entity.Spu;
import com.example.demo.pojo.*;
import com.example.demo.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class ShoppingContorller {



    @Autowired
    SkuService skuService;
    @Autowired
    SpuService spuService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    CartService cartService;
    @Autowired
    OrderService orderService;



    @GetMapping("/getCategory")
    public R getCategory(  String name){
        QueryWrapper<Category> qw=new QueryWrapper<>();
        qw.lambda().eq(Category::getName,name);
        if (name!=null){
            List<Category> categories = categoryService.getBaseMapper().selectList(qw);
            return R.success(categories);
        }
        else {
            List<Category> categories = categoryService.getBaseMapper().selectList(null);
            return R.success(categories);
        }

    }

    @GetMapping("/get")
    public R getGood(Integer id ,Integer pageNo,Integer pageSize){
        System.out.println(id);
        QueryWrapper<Spu> qw=new QueryWrapper<>();
        qw.lambda().eq(Spu::getCategoryId,id);
        List<Spu> spulesses = spuService.getBaseMapper().selectList(qw);
        List<Goods> good=new ArrayList<>();
        for (Spu spuless : spulesses) {
            List<Sku> sku = skuService.findSku(spuless.getId());
            Spu spu = spuService.findSpu(spuless.getId());
            Goods goods=new Goods();
            BeanUtils.copyProperties(spu,goods);
            goods.setSku(sku);
            good.add(goods);


        }
        List<Goods> r=new ArrayList<Goods>();
        for (int i=(pageNo-1)*pageSize; i<pageNo*pageSize;i++){
            if (i<good.size()){
                r.add(good.get(i));
            }
            else {
                break;
            }
        }
        System.out.println(r);
        return R.success(new ShopPVo(r,good.size()));
    }
    @PostMapping("/addCart")
    public R addCart(Cart cart){
        System.out.println(cart);
        QueryWrapper<Cart> qw=new QueryWrapper<>();
        qw.lambda().eq(Cart::getCId,cart.getCId());
        qw.lambda().eq(Cart::getStatus,1);
        qw.lambda().eq(Cart::getSpu,cart.getSpu());
        qw.lambda().eq(Cart::getSku,cart.getSku());
        Cart cart1 = cartService.getBaseMapper().selectOne(qw);
        if (cart1==null){
            cartService.getBaseMapper().insert(cart);
        }
        else {
            cart1.setNum(cart1.getNum()+cart.getNum());
            cartService.getBaseMapper().updateById(cart1);
        }

        return R.success("成功");
    }
    @GetMapping("/selectCart")
    public R selectCart(Integer cid){
        QueryWrapper<Cart> qw=new QueryWrapper<>();
        qw.lambda().eq(Cart::getCId,cid);
        qw.lambda().eq(Cart::getStatus,1);
        List<Cart> carts = cartService.getBaseMapper().selectList(qw);
        List<CartVo> vos= new ArrayList<>();
        for (Cart cart : carts) {
            QueryWrapper<Spu> queryWrapper=new QueryWrapper();
            queryWrapper.lambda().eq(Spu::getId,cart.getSpu());
            Spu spu = spuService.getBaseMapper().selectOne(queryWrapper);
            CartVo vo=new CartVo();
            BeanUtils.copyProperties(cart,vo);
            vo.setSpuName(spu.getName());
            vos.add(vo);
        }

        return R.success(vos);
    }
    @GetMapping("/getSpuName")
    public R getName(Integer spuId){
        QueryWrapper<Spu> qw=new QueryWrapper<>();
        qw.lambda().eq(Spu::getId,spuId);
        Spu spu = spuService.getBaseMapper().selectOne(qw);
        return R.success(spu.getName());
    }

    @PostMapping("/pay")
    public R pay(@RequestBody ArrayList<Integer> id){
        for (Integer i=0 ; i<id.size();i++) {
            QueryWrapper<Cart> qw=new QueryWrapper<>();
            qw.lambda().eq(Cart::getId,id.get(i));
            Cart cart = cartService.getBaseMapper().selectOne(qw);
            Orders order=new Orders();
            BeanUtils.copyProperties(cart,order);
            cart.setStatus(0);
            cartService.getBaseMapper().updateById(cart);

            orderService.getBaseMapper().insert(order);
        }
        return R.success("支付成功");
    }

}

